﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace TBG.TBG_F
{
    class mAttack
    {
        Texture2D _Texture;
        int frameWidth = 48;
        int frameCount = 9;
        int currentFrame = 0;

        float frameRate = 0.05f;
        float fElapsed = 0.0f;

        public Vector2 currentPosition;
        public Vector2 destinationPosition;

        public bool shoot = false;
        Vector2 speed = new Vector2(5, 0);
        float speedRate = 0.5f;
        float speedElapsed = .0f;

        Player player;
        AI enemy;

        public mAttack(ContentManager content)
        {
            _Texture = content.Load<Texture2D>("explossion");
        }

        public void Update(GameTime gameTime)
        {
            fElapsed += (float)gameTime.ElapsedGameTime.TotalSeconds;
            speedElapsed += (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (shoot)
            {
                if (fElapsed >= frameRate)
                {
                    currentFrame = (currentFrame + 1) % frameCount;
                    fElapsed = .0f;

                    if (destinationPosition.X > currentPosition.X)
                    {
                        //if (speedElapsed >= speedRate)
                        //{
                        //    speed.X++;
                        //}
                        currentPosition += speed;
                    }

                    if (destinationPosition.X <= currentPosition.X)
                    {
                        enemy.calculateHP(player._ATTACK);
                        shoot = false;
                    }
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            if(shoot)
                spriteBatch.Draw(_Texture, getLocationRect(), getSourceRect(), Color.White);
        }

        private Rectangle getSourceRect()
        {
            return new Rectangle(currentFrame * frameWidth, 0, frameWidth, 48);
        }

        public Rectangle getLocationRect()
        {
            return new Rectangle((int)currentPosition.X, (int)currentPosition.Y, frameWidth, frameWidth);
        }

        public void shootAI(Player player, AI Ai)
        {
            this.player = player;
            this.enemy = Ai;
            shoot = true;
            currentPosition = new Vector2(player.getPosition().X, player.getPosition().Y);
            destinationPosition = new Vector2(Ai.getPosition().X+2, Ai.getPosition().Y);
        }

        public void shootPlayer(Player player)
        {
            shoot = true;
            destinationPosition = new Vector2(player.getPosition().X, player.getPosition().Y);
        }
    }
}
